package cn.edu.dlut.ssdut.foodCredit2012.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import cn.edu.dlut.ssdut.foodCredit2012.model.UserCollection;
import cn.edu.dlut.ssdut.foodCredit2012.util.HibernateUtil;

public class UserCollectionService {
	BasicCollectionService basicCS = new BasicCollectionService();
	CustomsCollectionService customsCS = new CustomsCollectionService();
	FinanceCollectionService financeCS = new FinanceCollectionService();
	ExportCollectionService exportCS = new ExportCollectionService();
	PermissionCollectionService permissionCS = new PermissionCollectionService();
	PresentationCollectionService presentationCS = new PresentationCollectionService();
	QualityCollectionService qualityCS = new QualityCollectionService();
	TaxCollectionService taxCS = new TaxCollectionService();
	
	UserService userS = new UserService();
	
	public void add(UserCollection userC) {
		if(!isUserCollection(userC)){
			Session session = HibernateUtil.getCollectionSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.save(userC);
			session.getTransaction().commit();
		}
	}
	
	public UserCollection load(UserCollection userC)throws Exception {
		if(isUserCollection(userC)){
			userC = loadByLicenceID(userC.getLicenceID());
			return userC;
		}
		return userC;
	}
	
	
	public UserCollection loadByLicenceID(String licenceID) throws Exception{
		UserCollection userC = new UserCollection();
		Session session = HibernateUtil.getCollectionSessionFactory().getCurrentSession();
		session.beginTransaction();
		userC = (UserCollection)session.createQuery("select userC from UserCollection userC where userC.licenceID = :licenceID")
			.setParameter("licenceID", licenceID)
			.uniqueResult();
		session.getTransaction().commit();
		return userC;
	} 
	
	
	
	public void update(UserCollection userC) throws Exception{
		if(isUserCollection(userC)){
			Session session = HibernateUtil.getCollectionSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.update(userC);
			session.getTransaction().commit();
		}
	}
	
	
	
	public List<UserCollection> userCollectionList() {
		List<UserCollection> userCList = null;
		Session session = HibernateUtil.getCollectionSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		Query query = session.createQuery("from UserCollection");
		query.setFirstResult(1);
		query.setMaxResults(10);
		userCList = (List<UserCollection>)query.list();
		
		session.getTransaction().commit();
		
		return userCList;
	}
	
	public void delete(UserCollection userC) throws Exception{
		if(isUserCollection(userC)){
			Session session = HibernateUtil.getCollectionSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.delete(userC);
			session.getTransaction().commit();
		}
	}
	
	
	
	public boolean isUserCollection(UserCollection userC){
		Session session = HibernateUtil.getCollectionSessionFactory().getCurrentSession();
		session.beginTransaction();
		long count = (Long)session.createQuery("select count(*) from UserCollection u where u.licenceID = :licenceID")
			.setString("licenceID", userC.getLicenceID())
			.uniqueResult();
		session.getTransaction().commit();
		
		if(count > 0) return true;
		return false;
	}
}
